import { acceptHMRUpdate, defineStore } from 'pinia';

interface UserStore {
  userInfo: BasicUserInfo | null;
  userRole: BasicUserRoles[];
}

/**
 * 用户信息
 */
export const useUserStore = defineStore('userStore', {
  state: (): UserStore => ({
    userInfo: null,
    userRole: [],
  }),
  persist: {
    // 持久化
    pick: ['userInfo', 'userRole'],
  },
  actions: {
    setUserInfo(userInfo: BasicUserInfo | null) {
      this.userInfo = userInfo;
      const roles = userInfo?.roles ?? [];
      this.setUserRole(roles);
    },
    setUserRole(roles: BasicUserRoles[]) {
      this.userRole = roles;
    },
  },
});
// 解决热更新问题
const hot = import.meta.hot;
if (hot) {
  hot.accept(acceptHMRUpdate(useUserStore, hot));
}
